package leetcode_数组;

/**
 * 乘积最大子数组
 * <p>算法: 贪心</p>
 *
 * @author yezh
 * @date 2023/8/10 15:04
 */
public class _152 {

    public int maxProduct(int[] nums) {
        // 对于每个元素, 要么乘上这个元素, 要么直接拿这个元素
        int ans = nums[0], min = nums[0], max = nums[0], tmp1, tmp2;
        for (int i = 1; i < nums.length; i++) {
            tmp1 = min * nums[i];
            tmp2 = max * nums[i];
            max = Math.max(tmp1, Math.max(tmp2, nums[i]));
            min = Math.min(tmp1, Math.min(tmp2, nums[i]));
            ans = Math.max(ans, max);
        }
        return ans;
    }

}
